from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse
from django.template.loader import get_template
# Create your views here.
from .models import Student
from datetime import datetime
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required, permission_required
# def index(request):
#     now = datetime.now()
#     L = [1, 2, 3]
#     D = {'name': '权哥', 'age': 23, 'high': 170}
#     now = now.strftime('%Y年%m月%d日 %H:%M:%S')
#     format_str = '%Y年%m月%d日 %H:%M:%S'
#
#     def aaa():
#         return '哈哈哈'
#
#     html = '<h1>我是安全<h1>'
#     # abc = Student.objects.all()
#     abc = [
#         {'name': '一一一', 'age': 18, 'sex': 1, 'course': ['c++', 'python', 'java']},
#         {'name': '二二二', 'age': 19, 'sex': 0, 'course': ['c++', 'python', 'java']},
#         {'name': '三三三', 'age': 20, 'sex': 1, 'course': ['c++', 'python', 'java']},
#         {'name': '四四四', 'age': 20, 'sex': 0, 'course': ['c++', 'python', 'java']},
#     ]
#
#     return render(request, 'teacher/index.html', context={
#         'now': now,
#         'L': L,
#         'D': D,
#         'aaa': aaa,
#         'html': html,
#         'abc': abc,
#         'format_str': format_str,
#     })

def index(request):
    name = request.session.get('name', '游客')
    return render(request, 'teacher/new_index.html', context={
        'name': name,
    })


def login1(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        if username == '123' and password == '123':
            request.session['name'] = username
            request.session.set_expiry(10)
            return redirect(reverse('teacher:index'))
    return render(request, 'teacher/login.html')


# def logout(request):
#     request.session.flush()
#     return render(request, 'teacher/login.html')


def hao(request, a):

    return HttpResponse('这是{}的页面'.format(a))

@login_required
def ad(request):
    # if not request.user.is_authenticated:  # 未登录就会重定向到登录界面
    #     return redirect(reverse('teacher:login')+ ('?next={}'.format(request.path_info)))

    return render(request, 'teacher/ad.html')

from teacher.forms import RegisterForm


def rigister(request):
    form = RegisterForm()
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            password_repeat = form.cleaned_data.get('password_repeat')
            email = form.cleaned_data.get('email')
            if password == password_repeat:
                return HttpResponse('注册成功')

    return render(request, 'teacher/register.html', context={
        'form': form,
    })

def login_view(request):
    #user = request.user  # 后台登录后，这里的user是登录的用户名，退出后登录后这里是游客身份
    # if request.user.is_authenticated:   #匿名用户返回False
    #     return redirect(reverse('teacher:index'))
    ad = request.GET.get('next')
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)  # 校验用户和密码
        if user is not None:
            login(request, user)  # 登录,将用户信息保存到session中去
            if ad:
                return redirect(ad)
            return redirect(reverse('teacher:index'))
    return render(request, 'teacher/login.html')

def logout_view(request):
    logout(request)
    return redirect(reverse('teacher:login'))